<template>
  <ea-dialog
    v-bind="$attrs"
    v-on="$listeners"
    width="500px"
    class="dialog"
    ref="elDialog"
    :title="title"
    :type="type"
    @submit="uploadAvatar"
    @reset="$refs.form.resetFields()"
    @closed="handleClosed"
  >
    <el-scrollbar wrap-class="scroll-wrap-y">
      <el-form
        ref="form"
        :rules="rules"
        :model="singleData"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="选择学校" prop="schoolCode">
          <el-select
            v-model="singleData.schoolCode"
            @change="getDepAndClass"
            filterable
            placeholder="请选择学校"
          >
            <el-option
              v-for="item in schoolList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="classCode">
          <el-cascader
            :options="deptClass"
            :key="new Date().getTime()"
            v-model="singleData.classCode"
            :show-all-levels="true"
            placeholder="请选择班级"
            ref="cascaderAddr"
            @change="orderChange"
            :props="{
              multiple: isTeacher,
              value: 'code',
              label: 'name',
              emitPath: false
            }"
            collapse-tags
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="账号" prop="usercode"
          ><el-input
            v-model="singleData.usercode"
            placeholder="请输入帐号"
          ></el-input
        ></el-form-item>
        <el-form-item v-if="type === 'add'" label="密码" prop="password"
          ><el-input
            type="password"
            v-model="singleData.password"
            placeholder="请输入密码"
          ></el-input
        ></el-form-item>
        <el-form-item label="姓名" prop="username"
          ><el-input
            v-model="singleData.username"
            placeholder="请输入姓名"
          ></el-input
        ></el-form-item>
        <el-form-item label="手机" prop="phone"
          ><el-input
            v-model="singleData.phone"
            placeholder="请输入手机号"
          ></el-input
        ></el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="singleData.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生日" prop="birthday"
          ><el-date-picker
            v-model="singleData.birthday"
            type="date"
            placeholder="选择生日"
          ></el-date-picker
        ></el-form-item>
        <el-form-item label="描述" prop="description"
          ><el-input v-model="singleData.description"></el-input
        ></el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-upload
            ref="uploadAvatar"
            class="avatar-uploader"
            :action="uploadApi"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :auto-upload="false"
            list-type="picture"
            :on-change="choseAvatar"
          >
            <el-avatar :src="avatar"></el-avatar>
          </el-upload>
        </el-form-item>
      </el-form>
    </el-scrollbar>
  </ea-dialog>
</template>

<script>
import editAndAdd from "@/mixins/business/editAndAdd";

export default {
  name: "EaUser",
  mixins: [editAndAdd],
  props: {
    moduleName: {
      type: String,
      required: true
    },
    uploadApi: {
      type: String,
      required: true
    },
    submitApi: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      singleData: {
        avatar: "",
        birthday: "",
        classCode: "",
        description: "",
        password: "",
        phone: "",
        schoolCode: "",
        sex: 0,
        usercode: "",
        userid: 0,
        username: ""
      },
      rules: {
        classCode: { required: true, message: "请选择班级！" },
        schoolCode: { required: true, message: "请选择学校！" },
        usercode: { required: true, message: "请输入帐号！" },
        username: { required: true, message: "请输入姓名！" },
        password: { required: true, message: "请输入密码！" }
      },
      loading: false,
      preview: null,
      fileReader: null,
      deptClass: [
        {
          code: "00201",
          name: "旅游系",
          pcode: "002",
          children: [
            {
              children: null,
              code: "0020101",
              name: "旅游三班",
              pcode: "00201"
            }
          ]
        },
        {
          code: "00202",
          name: "经管系",
          pcode: "002",
          children: null
        }
      ],
      schoolList: []
    };
  },
  created() {
    if (this.isTeacher) this.rules.classCode.required = false;
    this.fileReader = new FileReader();
    this.fileReader.onload = res => {
      this.preview = res.target.result;
    };
    this.api.getSchool().then(res => {
      this.schoolList = res;
    });
  },
  computed: {
    code: {
      get() {
        return this.singleData.classCode[0];
      },
      set(v) {}
    },
    avatar() {
      if (this.preview) return this.preview;
      return this.singleData.avatar;
    },
    isTeacher() {
      return this.moduleName === "教师";
    }
  },
  methods: {
    orderChange(e) {
      const deepArr = this.deptClass;
      // 学员CODE一起传入
      for (var j = 0; j < deepArr.length; j++) {
        if (!deepArr[j].children) {
          return;
        }
        for (var i = 0; i < deepArr[j].children.length; i++) {
          if (deepArr[j].children[i].code == this.singleData.classCode[0]) {
            this.singleData.classCode.push(deepArr[j].code);
          }
        }
      }
    },
    uploadAvatar() {
      if (this.preview) {
        this.$refs.uploadAvatar.submit();
        this.$refs.elDialog.$data.loading = true;
      } else this.submit();
    },
    handle() {
      if (this.type === "add") this.singleData.userid = null;
      const singleData = this.deepClone(this.singleData);
      if (!singleData.classCode) singleData.classCode = null;
      return this.axios.post(this.submitApi, singleData);
    },
    choseAvatar(file) {
      this.fileReader.readAsDataURL(file.raw);
    },
    handleAvatarSuccess(res) {
      this.singleData.avatar = res.data;
      this.$refs.uploadAvatar.clearFiles();
      this.submit();
    },
    getDepAndClass(schoolCode) {
      this.axios
        .get(`/sys/class/getDeptClassBySchool/${schoolCode}`)
        .then(res => {
          this.deptClass = res;
        });
    },
    handleClosed() {
      this.singleData.userid = null;
      this.preview = null;
      this.deptClass = [];
      this.$refs.uploadAvatar.clearFiles();
      this.end();
    }
  }
};
</script>

<style scoped></style>
